﻿using CastingWellEquipmentGUI.Config;
using CastingWellEquipmentGUI.Model;
using CastingWellEquipmentGUI.Utils;
using CastingWellEquipmentGUI.ViewModel.Common.Command;
using CastingWellEquipmentGUI.ViewModel.Common.WindowsManager;
using Confluent.Kafka;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace CastingWellEquipmentGUI.ViewModel.Pages
{
    public class WaterSupplySystemVireModel : ViewModelBase
    {
        #region 变量/常量
        /// <summary>
        /// 当前窗体名称
        /// </summary>
        private const string showPageTitle = "WaterSupplySystemPage";
        private string _CurrentControl;
        /// <summary>
        /// 当前选中的控件
        /// </summary>
        public string CurrentControl
        {
            get { return _CurrentControl; }
            set { _CurrentControl = value; RaisePropertyChanged("CurrentControl"); }
        }
        #endregion 变量/常量

        #region 页面属性
        #region Label显示
        /// <summary>
        /// 进水压力显示
        /// </summary>
        public string Lable_WaterPressurePV1
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Lable_WaterPressurePV1"); }
        }
        /// <summary>
        /// 紧急水压力显示
        /// </summary>
        public string Lable_WaterPressurePV2
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Lable_WaterPressurePV2"); }
        }
        /// <summary>
        /// PV温度显示
        /// </summary>
        public string Lable_Temperature
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Lable_Temperature"); }
        }
        /// <summary>
        /// PV百分比显示
        /// </summary>
        public string Lable_Percentage
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Lable_Percentage"); }
        }
        /// <summary>
        /// PV流量显示
        /// </summary>
        public string Lable_FlowPV
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Lable_FlowPV"); }
        }
        /// <summary>
        /// SP流量显示
        /// </summary>
        public string Lable_FlowSP
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Lable_FlowSP"); }
        }
        #endregion Label显示

        #region Button显示
        private string _Button_CVValue;
        /// <summary>
        /// CV值设定
        /// </summary>
        public string Button_CVValue
        {
            get { return _Button_CVValue; }
            set
            {
                if (KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_CVValue", value))
                {
                    _Button_CVValue = value;
                    RaisePropertyChanged("Button_CVValue");
                }
            }
        }
        private string _Button_CIPTapImageUrl = "";
        /// <summary>
        /// 反冲洗装置图片路径
        /// </summary>
        public string Button_CIPTapImageUrl
        {
            get { return _Button_CIPTapImageUrl; }
            set { _Button_CIPTapImageUrl = value; RaisePropertyChanged("Button_CIPTapImageUrl"); }
        }
        private string _Button_RateTapImageUrl = "";
        /// <summary>
        /// 供水比例阀图片路径
        /// </summary>
        public string Button_RateTapImageUrl
        {
            get { return _Button_RateTapImageUrl; }
            set { _Button_RateTapImageUrl = value; RaisePropertyChanged("Button_RateTapImageUrl"); }
        }
        #endregion Button显示

        #region 预检测项目
        private Visibility _PreDetectionVisibility;
        /// <summary>
        /// 预检测状态
        /// 注：控制预检测框显隐
        /// </summary>
        public Visibility PreDetectionVisibility
        {
            get { return _PreDetectionVisibility; }
            set { _PreDetectionVisibility = value; RaisePropertyChanged("PreDetectionVisibility"); }
        }
        /// <summary>
        /// 水系统冲洗完成
        /// </summary>
        public Brush Label_PreCheck1
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck1") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 水阀架电器柜温度正常
        /// </summary>
        public Brush Label_PreCheck2
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck2") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 紧急水压力正常
        /// </summary>
        public Brush Label_PreCheck3
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck3") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 紧急水阀关闭
        /// </summary>
        public Brush Label_PreCheck4
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck4") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 紧急水阀测试正常
        /// </summary>
        public Brush Label_PreCheck5
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck5") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 反冲洗过滤器未堵塞
        /// </summary>
        public Brush Label_PreCheck6
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck6") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 反冲洗过滤器正常
        /// </summary>
        public Brush Label_PreCheck7
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck7") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 反冲洗过滤器阀门关闭
        /// </summary>
        public Brush Label_PreCheck8
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck8") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 供水阀门已打开
        /// </summary>
        public Brush Label_PreCheck9
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck9") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 水流量正常
        /// </summary>
        public Brush Label_PreCheck10
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck10") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 铸造水温正常
        /// </summary>
        public Brush Label_PreCheck11
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck11") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        /// <summary>
        /// 供水比例阀阀芯正常
        /// </summary>
        public Brush Label_PreCheck12
        {
            get
            {
                if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_PreCheck12") == "true")
                {
                    return Brushes.Red;
                }
                return Brushes.Black;
            }
        }
        #endregion 预检测项目

        #region 阀门图片设置

        #region 排水管道
        /// <summary>
        /// 排水管道 0关闭、1关闭中、2打开中、3已打开
        /// </summary>
        private string drainagePipeTagValue = "0";
        private string _Image_DrainagePipeUrl = "";
        /// <summary>
        /// 排水管道
        /// </summary>
        public string Image_DrainagePipeUrl
        {
            get { return _Image_DrainagePipeUrl; }
            set { _Image_DrainagePipeUrl = value; RaisePropertyChanged("Image_DrainagePipeUrl"); }
        }
        #endregion 排水管道

        #region 铸盘供水阀
        /// <summary>
        /// 铸盘供水阀 0关闭、1关闭中、2打开中、3已打开
        /// </summary>
        private string castWaterTagValue = "0";
        private string _Image_CastWaterUrl = "";
        /// <summary>
        /// 铸盘供水阀
        /// </summary>
        public string Image_CastWaterUrl
        {
            get { return _Image_CastWaterUrl; }
            set { _Image_CastWaterUrl = value; RaisePropertyChanged("Image_CastWaterUrl"); }
        }
        #endregion 铸盘供水阀

        #region 紧急水阀
        /// <summary>
        /// 紧急水阀 0关闭、1关闭中、2打开中、3已打开
        /// </summary>
        private string urgentTapTagValue = "0";
        private string _Button_UrgentTapImageUrl = "";
        /// <summary>
        /// 紧急水阀
        /// </summary>
        public string Button_UrgentTapImageUrl
        {
            get { return _Button_UrgentTapImageUrl; }
            set { _Button_UrgentTapImageUrl = value; RaisePropertyChanged("Button_UrgentTapImageUrl"); }
        }
        #endregion 紧急水阀

        #endregion 阀门图片设置
        #endregion 页面属性
        #region 定义页面执行命令
        /// <summary>
        /// PV数值设定按钮命令
        /// </summary>
        public DelegateCommand ShowModalPanelCommand { get; set; }
        /// <summary>
        /// 反冲洗装置打开关闭
        /// </summary>
        public DelegateCommand CIPTapCommand { get; set; }
        /// <summary>
        /// 供水比例阀
        /// </summary>
        public DelegateCommand RateTapCommand { get; set; }
        /// <summary>
        /// 流量计设置命令
        /// </summary>
        public DelegateCommand ShowFlowmeterSetPanelCommand { get; set; }
        /// <summary>
        /// 质量流量计(递增或递减)命令
        /// </summary>
        public DelegateCommand SetSelectControlTagValueByCalculationCommand { get; set; }
        /// <summary>
        /// 隐藏手动自动切换窗体命令
        /// </summary>
        public DelegateCommand HideFeederPageSettingPanel1WindowCommand { get; set; }
        /// <summary>
        /// 紧急水阀命令
        /// </summary>
        public DelegateCommand ShowUrgentTapSetPanelCommand { get; set; }
        /// <summary>
        /// 跳转页面名称
        /// </summary>
        public DelegateCommand GoToPageCommand { get; set; }
        #endregion 定义页面执行命令

        #region 线程对象
        /// <summary>
        /// 动画线程
        /// </summary>
        private Thread animationThread = null;
        /// <summary>
        /// 标签值变化监听线程
        /// </summary>
        private Thread tagInfoSyncThread = null;
        #endregion 线程对象

        #region 方法
        /// <summary>
        /// 构造函数
        /// </summary>
        public WaterSupplySystemVireModel()
        {
            // 初始化命令
            initCommand();
            //初始化线程
            initThread();
            // 消息通道注册
            initMessenger();
            // 初始化基础信息
            initBaseSet();
        }

        /// <summary>
        /// 初始化线程
        /// </summary>
        private void initThread()
        {
            // 动画线程
            animationThread = new Thread(animationThreadFunc);
            animationThread.SetApartmentState(ApartmentState.STA);
            animationThread.IsBackground = true;
            animationThread.Start();

            // 数据线程
            tagInfoSyncThread = new Thread(syncThreadFunc);
            tagInfoSyncThread.SetApartmentState(ApartmentState.STA);
            tagInfoSyncThread.IsBackground = true;
            tagInfoSyncThread.Start();
        }

        /// <summary>
        /// 初始化命令
        /// </summary>
        private void initCommand()
        {
            // 页面跳转
            GoToPageCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(changeTabHandler)
            };
            // 数值设定按钮命令
            ShowModalPanelCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(showModalPanelHandler)
            };
            // 反冲洗装置打开关闭
            CIPTapCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(CIPTapCommandHandler)
            };
            // 供水比例阀
            RateTapCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(rateTapCommandHandler)
            };
            // 流量计设置命令
            ShowFlowmeterSetPanelCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(showFlowmeterSetPanelCommandHandler)
            };
            // 质量流量计(递增或递减)命令
            SetSelectControlTagValueByCalculationCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(setSelectControlTagValueByCalculationHandler)
            };
            // 关闭流量计设置弹框
            HideFeederPageSettingPanel1WindowCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(hidePageSettingPanel1Handler)
            };
            // 紧急水阀命令
            ShowUrgentTapSetPanelCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(urgentTapCommandHandler)
            };
        }

        /// <summary>
        /// 页面切换
        /// </summary>
        /// <param name="obj"></param>
        private void changeTabHandler(object obj)
        {
            Messenger.Default.Send<string>(obj.ToString(), "GoToPage");
        }

        /// <summary>
        /// 紧急水阀命令
        /// </summary>
        /// <param name="obj"></param>
        private void urgentTapCommandHandler(object obj)
        {
            // 获取参数，此处参数为按钮名称
            string param = obj.ToString();
            WindowManager.ShowModalTestOrOnOrOffPanel("TestOrOnOrOffPanel", showPageTitle + param);
        }

        /// <summary>
        /// 消息通道注册
        /// ViewModel之间数据通信使用，整个项目全局唯一
        /// </summary>
        private void initMessenger()
        {
            // CV值设定
            Messenger.Default.Register<string>(this, showPageTitle + "Button_CVValue", (message) =>
            {
                Button_CVValue = message.ToString();
            });
            // 反冲洗装置开启或关闭
            Messenger.Default.Register<string>(this, showPageTitle + "Button_CIPTap", (message) =>
            {
                setCIPTapOnOrOff(message.ToString());
            });
            // 供水比例阀自动和手动
            Messenger.Default.Register<string>(this, showPageTitle + "Button_RateTap", (message) =>
            {
                setRateTapHandOrAuto(message.ToString());
            });
            // 紧急水阀
            Messenger.Default.Register<string>(this, showPageTitle + "Button_UrgentTap", (message) =>
            {
                setUrgentTapOnOrOff(message.ToString());
            });
            //页面切换执行控件刷新
            Messenger.Default.Register<string>(this, showPageTitle + "_DoChange", (message) =>
            {
                HashSet<Control> tempHashSet = SysCommon.getCurrentControl(showPageTitle);
                if (tempHashSet.Count > 0)
                {
                    DoChanged(tempHashSet);
                }
            });
        }

        /// <summary>
        /// 紧急水阀
        /// </summary>
        /// <param name="message">True Or False</param>
        private void setUrgentTapOnOrOff(string message)
        {
            if (message == "" || message == null)
            {
                return;
            }
            if (message == "True")
            {
                KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_UrgentTap_Open", "true");
            }
            else if (message == "False")
            {
                KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_UrgentTap_Close", "true");
            }
            else if (message == "Test")
            {
                KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_UrgentTap_Test", "true");
            }
        }

        /// <summary>
        /// 质量流量计设置命令
        /// </summary>
        /// <param name="obj"></param>
        private void showFlowmeterSetPanelCommandHandler(object obj)
        {
            CurrentControl = obj.ToString();
            WindowManager.ShowDialog("FlowmeterSetPanelTow", null);
        }

        /// <summary>
        /// 供水比例阀自动和手动
        /// </summary>
        /// <param name="message">True Or False</param>
        private void setRateTapHandOrAuto(string message)
        {
            if (message == "" || message == null)
            {
                return;
            }
            if (message == "True")
            {
                // 手动
                if (KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_RateTap_Hand", "true"))
                {
                    Button_RateTapImageUrl = "/images/WaterSupplySystem/LowSpeedValveHand.png";
                }
            }
            else if (message == "False")
            {
                // 自动
                if (KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_RateTap_Auto", "true"))
                {
                    Button_RateTapImageUrl = "/images/WaterSupplySystem/LowSpeedValveAuto.png";
                }
            }
        }

        /// <summary>
        /// 初始化基础信息
        /// </summary>
        private void initBaseSet()
        {
            // 页面初始化
            HashSet<Control> tempHashSet = SysCommon.getCurrentControl(showPageTitle);
            if (tempHashSet.Count > 0)
            {
                DoChanged(tempHashSet);
            }
            /** 设定值初始化 **/
            _Button_CVValue = TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Button_CVValue");
            RaisePropertyChanged("Button_CVValue");
        }

        /// <summary>
        /// 数据线程
        /// </summary>
        private void syncThreadFunc()
        { // 创建消费者对象
            IConsumer<string, object> KafkaConsumer = KafkaHelper.GetConsumer();
            while (true)
            {
                if (SystemConfig.systemState == "预检测" || SystemConfig.systemState == "可见许可")
                {
                    PreDetectionVisibility = Visibility.Visible;
                }
                else
                {
                    PreDetectionVisibility = Visibility.Hidden;
                }
                // 网络中断不进行消费监听
                if (SystemConfig.SystemStatus == "网络中断")
                {
                    Thread.Sleep(1000);
                    continue;
                }
                if (ThreadController.RoundBarWaterSupplySystemThreadState)
                {
                    //开始消费
                    var result = KafkaConsumer.Consume();
                    if (result != null)
                    {
                        //手动提交
                        KafkaConsumer.Commit(result);
                        //获取消息内容
                        string tagId = result.Message.Value.ToString();
                        HashSet<Control> hashSet = KafkaHelper.UpdateCurrentUserTagValue(Int32.Parse(tagId), KafkaHelper.GetTagValueByKey(tagId.ToString()), showPageTitle);
                        if (hashSet != null && hashSet.Count > 0)
                        {
                            DoChanged(hashSet);
                        }
                    }
                }
                Thread.Sleep(10);
            }
        }

        /// <summary>
        /// 数值设定按钮命令
        /// </summary>
        /// <param name="obj"></param>
        private void showModalPanelHandler(object obj)
        {
            string param = obj.ToString();
            switch (param)
            {
                // PV温度设定设置
                case "Button_CVValue":
                    WindowManager.ShowModalDigitalInputPanel("SetCVPanel", showPageTitle + "Button_CVValue", Button_CVValue);
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 反冲洗装置开启或关闭
        /// </summary>
        /// <param name="message">True Or False</param>
        private void setCIPTapOnOrOff(string message)
        {
            if (message == "" || message == null)
            {
                return;
            }
            if (message == "True")
            {
                if (KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_CIPTap_Open", "true"))
                {
                    Button_CIPTapImageUrl = "/images/WaterSupplySystem/WaterFiltersOpen.png";
                }
            }
            else if (message == "False")
            {
                if (KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_CIPTap_Close", "true"))
                {
                    Button_CIPTapImageUrl = "/images/WaterSupplySystem/WaterFiltersClose.png";
                }
            }
        }

        /// <summary>
        /// 反冲洗装置打开关闭
        /// </summary>
        /// <param name="obj"></param>
        private void CIPTapCommandHandler(object obj)
        {
            // 获取参数，此处参数为按钮名称
            string param = obj.ToString();
            WindowManager.ShowModalOnOrOffPanel("OnOrOffPanel", showPageTitle + param);
        }

        /// <summary>
        /// 动画线程
        /// </summary>
        private void animationThreadFunc()
        {
            while (true)
            {
                if (ThreadController.RoundBarWaterSupplySystemThreadState)
                {
                    /** 1、排水管道 动画**/
                    if (drainagePipeTagValue == "0")
                    {
                        // 关闭
                        Image_DrainagePipeUrl = "/images/WaterSupplySystem/ControlValveHorizontalRed.png";
                    }
                    else if (drainagePipeTagValue == "1")
                    {
                        // 关闭中(灰与红切换)
                        if (Image_DrainagePipeUrl == "/images/WaterSupplySystem/ControlValveHorizontal.png")
                        {
                            Image_DrainagePipeUrl = "/images/WaterSupplySystem/ControlValveHorizontalRed.png";
                        }
                        else
                        {
                            Image_DrainagePipeUrl = "/images/WaterSupplySystem/ControlValveHorizontal.png";
                        }
                    }
                    else if (drainagePipeTagValue == "2")
                    {
                        // 打开中(灰与绿切换)
                        if (Image_DrainagePipeUrl == "/images/WaterSupplySystem/ControlValveHorizontal.png")
                        {
                            Image_DrainagePipeUrl = "/images/WaterSupplySystem/ControlValveHorizontalGreen.png";
                        }
                        else
                        {
                            Image_DrainagePipeUrl = "/images/WaterSupplySystem/ControlValveHorizontal.png";
                        }
                    }
                    else if (drainagePipeTagValue == "3")
                    {
                        // 已打开
                        Image_DrainagePipeUrl = "/images/WaterSupplySystem/ControlValveHorizontalGreen.png";
                    }
                    /** 2、铸盘供水阀 动画**/
                    if (castWaterTagValue == "0")
                    {
                        // 关闭
                        Image_CastWaterUrl = "/images/WaterSupplySystem/ControlValveHorizontalRed.png";
                    }
                    else if (castWaterTagValue == "1")
                    {
                        // 关闭中(灰与红切换)
                        if (Image_CastWaterUrl == "/images/WaterSupplySystem/ControlValveHorizontal.png")
                        {
                            Image_CastWaterUrl = "/images/WaterSupplySystem/ControlValveHorizontalRed.png";
                        }
                        else
                        {
                            Image_CastWaterUrl = "/images/WaterSupplySystem/ControlValveHorizontal.png";
                        }
                    }
                    else if (castWaterTagValue == "2")
                    {
                        // 打开中(灰与绿切换)
                        if (Image_CastWaterUrl == "/images/WaterSupplySystem/ControlValveHorizontal.png")
                        {
                            Image_CastWaterUrl = "/images/WaterSupplySystem/ControlValveHorizontalGreen.png";
                        }
                        else
                        {
                            Image_CastWaterUrl = "/images/WaterSupplySystem/ControlValveHorizontal.png";
                        }
                    }
                    else if (castWaterTagValue == "3")
                    {
                        // 已打开
                        Image_CastWaterUrl = "/images/WaterSupplySystem/ControlValveHorizontalGreen.png";
                    }
                    /** 3、紧急水阀 动画**/
                    if (urgentTapTagValue == "0")
                    {
                        // 关闭
                        Button_UrgentTapImageUrl = "/images/WaterSupplySystem/ControlValveVerticalRed.png";
                    }
                    else if (urgentTapTagValue == "1")
                    {
                        // 关闭中(灰与红切换)
                        if (Button_UrgentTapImageUrl == "/images/WaterSupplySystem/ControlValveVertical.png")
                        {
                            Button_UrgentTapImageUrl = "/images/WaterSupplySystem/ControlValveVerticalRed.png";
                        }
                        else
                        {
                            Button_UrgentTapImageUrl = "/images/WaterSupplySystem/ControlValveVertical.png";
                        }
                    }
                    else if (urgentTapTagValue == "2")
                    {
                        // 打开中(灰与绿切换)
                        if (Button_UrgentTapImageUrl == "/images/WaterSupplySystem/ControlValveVertical.png")
                        {
                            Button_UrgentTapImageUrl = "/images/WaterSupplySystem/ControlValveVerticalGreen.png";
                        }
                        else
                        {
                            Button_UrgentTapImageUrl = "/images/WaterSupplySystem/ControlValveVertical.png";
                        }
                    }
                    else if (urgentTapTagValue == "3")
                    {
                        // 已打开
                        Button_UrgentTapImageUrl = "/images/WaterSupplySystem/ControlValveVerticalGreen.png";
                    }
                }
                Thread.Sleep(10);
            }
        }

        /// <summary>
        /// 供水比例阀
        /// </summary>
        /// <param name="obj"></param>
        private void rateTapCommandHandler(object obj)
        {
            // 获取参数，此处参数为按钮名称
            string param = obj.ToString();
            WindowManager.ShowModalHandOrAutoPanel("HandOrAutoPanel", showPageTitle + param);
        }

        /// <summary>
        /// 流量计(递增或递减)命令处理器
        /// </summary>
        /// <param name="obj"></param>
        private void setSelectControlTagValueByCalculationHandler(object obj)
        {
            // 点击增加或减少按钮(+100,+300,-100,-300)
            string para = obj.ToString();
            // 设定标签信息
            BusinessCmd setTagValueMsg = new BusinessCmd();
            setTagValueMsg.Guid = Guid.NewGuid().ToString();
            setTagValueMsg.BusinessCmdCode = 3;
            // 流量计1按钮
            if (para == "+100")
            {
                setTagValueMsg.TagId = TagTool.GetTagIdByValueByFormNameAndControlName(showPageTitle, "FlowmeterPlus1");
                setTagValueMsg.TagValue = "true";
            }
            else if (para == "+300")
            {
                setTagValueMsg.TagId = TagTool.GetTagIdByValueByFormNameAndControlName(showPageTitle, "FlowmeterPlus2");
                setTagValueMsg.TagValue = "true";
            }
            else if (para == "-100")
            {
                setTagValueMsg.TagId = TagTool.GetTagIdByValueByFormNameAndControlName(showPageTitle, "FlowmeterSubtract1");
                setTagValueMsg.TagValue = "true";
            }
            else if (para == "-300")
            {
                setTagValueMsg.TagId = TagTool.GetTagIdByValueByFormNameAndControlName(showPageTitle, "FlowmeterSubtract2");
                setTagValueMsg.TagValue = "true";
            }
            // 发送消息
            if (setTagValueMsg.TagValue != null)
            {
                KafkaHelper.SendGatewayBusinessCmd(setTagValueMsg);
            }
        }

        /// <summary>
        /// 隐藏命令处理器
        /// </summary>
        /// <param name="obj"></param>
        private void hidePageSettingPanel1Handler(object obj)
        {
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "FlowmeterSetPanelTwoMessage"));
        }

        /// <summary>
        /// 执行动作
        /// </summary>
        /// <param name="hashSet"></param>
        private void DoChanged(HashSet<Control> hashSet)
        {
            //遍历返回的控件信息刷新对应的控件
            foreach (Control item in hashSet)
            {
                switch (item.ControlName)
                {

                    // 排水管道
                    case "Image_DrainagePipe":
                        drainagePipeTagValue = TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName);
                        break;
                    case "Image_CastWater":
                        castWaterTagValue = TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName);
                        break;
                    // 紧急水阀
                    case "Button_UrgentTapImage":
                        urgentTapTagValue = TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName);
                        break;
                    case "Button_CIPTap_Open":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) == "true")
                        {
                            Button_CIPTapImageUrl = "/images/WaterSupplySystem/WaterFiltersOpen.png";
                        }
                        break;
                    /** 反冲洗装置关闭 **/
                    case "Button_CIPTap_Close":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) == "true")
                        {
                            Button_CIPTapImageUrl = "/images/WaterSupplySystem/WaterFiltersClose.png";
                        }
                        break;
                    /** 供水比例阀自动 **/
                    case "Button_RateTap_Auto":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) == "true")
                        {
                            Button_RateTapImageUrl = "/images/WaterSupplySystem/LowSpeedValveAuto.png";
                        }
                        break;
                    /** 供水比例阀手动 **/
                    case "Button_RateTap_Hand":
                        if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, item.ControlName) == "true")
                        {
                            Button_RateTapImageUrl = "/images/WaterSupplySystem/LowSpeedValveHand.png";
                        }
                        break;
                    default:
                        /** 刷新页面值变化信息 **/
                        RaisePropertyChanged(item.ControlName);
                        break;
                }
            }
        }
        #endregion 方法

    }
}
